package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.Inandout;
import com.mrtan.bean.OrderSell;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.service.InandoutService;
import com.mrtan.service.OrderService;
import com.mrtan.service.StockService;
import com.mrtan.vo.OrderVO;
import com.mrtan.vo.OutVO;
import com.mrtan.vo.OutWhereVO;
import com.mrtan.vo.StockInsertVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/inandout")
public class InandOutController {
    @Resource
    private StockService stockService;
    @Resource
    private OrderService orderService;
    @Resource
    private InandoutService inandoutService;

    @GetMapping("/showsellw")
    public SaResult orderList(@RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit); // 在查询之前使用,会自动对紧接着的第一个查询进行分页
        List<OrderSell> list = orderService.orderListw();
        PageInfo<OrderSell> pageInfo = new PageInfo<>(list);
        PageBeanDto<OrderSell> pageBeanDto = new PageBeanDto();
        pageBeanDto.setList(pageInfo.getList());
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        return SaResult.ok()
                .setData(pageBeanDto);
    }

    //已处理销售订单查询
    @GetMapping("/showselly")
    public SaResult orderList2(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit); // 在查询之前使用,会自动对紧接着的第一个查询进行分页
        List<OrderSell> list = orderService.orderListy();
        PageInfo<OrderSell> pageInfo = new PageInfo<>(list);
        PageBeanDto<OrderSell> pageBeanDto = new PageBeanDto();
        pageBeanDto.setList(pageInfo.getList());
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        return SaResult.ok()
                .setData(pageBeanDto);
    }

    @PutMapping("/out")
    public SaResult out(OutVO outVO) {
        int add = inandoutService.add(outVO);
        if (add > 0) {
            return SaResult.ok();
        } else {
            return SaResult.error();
        }

    }

    @PostMapping("/updateOrderStatus")
    public SaResult updateOrderStatus(@RequestBody OrderVO orderVO) {
        int i = orderService.updateOrderStatus(orderVO);
        if (i > 0) {
            return SaResult.ok();
        } else {
            return SaResult.error();
        }
    }

    @PostMapping("/updateStock")
    public SaResult updateStock(@RequestBody StockInsertVO stockInsertVO) {
        int update = inandoutService.upateisinandout(stockInsertVO);
        int i = stockService.updateNumber(stockInsertVO);
        if (i > 0 && update > 0) {
            return SaResult.ok();
        } else {
            return SaResult.error();
        }
    }

    //查询出库表单
    @GetMapping("/selectout")
    public SaResult selectout(OutWhereVO outWhereVO,
                              @RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit);
        List<Inandout> outs = inandoutService.selectout(outWhereVO);
        for (Inandout out : outs) {
            out.setPrice(out.getSuggestedPrice().multiply(new BigDecimal(out.getQuantity())));
        }
        PageInfo<Inandout> pageInfo = new PageInfo<>(outs);
        PageBeanDto<Inandout> pageBeanDto = new PageBeanDto();
        pageBeanDto.setList(pageInfo.getList());
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        return SaResult.ok()
                .setData(pageBeanDto);
    }

}
